//
//  LCSubjectViewController.m
//  TianXiaShi
//
//  Created by CongCong on 15-7-13.
//  Copyright (c) 2015年 CongCong. All rights reserved.
//

#import "LCSubjectViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "LCSubjectModel.h"
#import "LCDetailCell.h"
#import "LCDetailViewController.h"
#define kSubUrl @"http://vcsp.ifeng.com/vcsp/appData/getDetailTopic.do?useType=androidPhone&itemType=%@&itemId=%@"
@interface LCSubjectViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) MPMoviePlayerController *player;
@property (nonatomic, strong) LCSubjectModel *subModel;
- (IBAction)back:(id)sender;
- (IBAction)collection:(id)sender;
- (IBAction)share:(id)sender;

@end

@implementation LCSubjectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.tabView.frame];
    imageView.image = [UIImage imageNamed:@"bgView"];
    self.tabView.backgroundView =imageView;
    [self.tabView registerNib:[UINib nibWithNibName:@"LCDetailCell" bundle:nil] forCellReuseIdentifier:@"LCDetailCell"];
    self.tabView.rowHeight = 80;
    if ([self.membertype isEqualToString:@"focus"]) {
        self.membertype = @"ifengFocus";
    }
    if ([self.membertype isEqualToString:@"cmpptopic"]) {
        self.membertype = @"cmppTopic";
    }
    NSLog(@"%@",[NSString stringWithFormat:kSubUrl,self.membertype,self.memberID]);
    [self downWebDataWithUrl:[NSString stringWithFormat:kSubUrl,self.membertype,self.memberID]];
    
    
}
- (void)initJosnModelWith:(NSDictionary *)dict
{
    self.subModel = [[LCSubjectModel alloc]initWithDictionary:dict error:nil];
    [self creatVideo];
    [self.tabView reloadData];
}
- (void)creatVideo
{
    LCSubTopListModel *topModel = self.subModel.subTopicList[0];
    LCSubDetailModel *detailModel = topModel.detailList[0];
    LCFilesModel *fileModel = detailModel.memberItem.files[1];
    NSString *url = fileModel.mediaUrl;
    NSRange range = [url rangeOfString:@"mp4"];
    if (range.location == NSNotFound) {
        fileModel = detailModel.memberItem.files[0];
        url = fileModel.mediaUrl;
    }
    
    self.player = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:url]];
    self.player.view.frame = self.videoView.frame;
    [self.videoView addSubview:[self.player view]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBack:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [self.player play];
    
}
- (void)playBack:(NSNotification *)nf
{
    NSLog(@"播放完毕");
    LCSubTopListModel *topModel = self.subModel.subTopicList[0];
    LCSubDetailModel *detailModel = topModel.detailList[1];
    LCDetailViewController *detail = [[LCDetailViewController alloc]init];
    detail.guid = detailModel.memberItem.guid;
    [self presentViewController:detail animated:YES completion:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.subModel.subTopicList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    LCSubTopListModel *topModel = self.subModel.subTopicList[section];
    return topModel.detailList.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LCSubTopListModel *topModel = self.subModel.subTopicList[indexPath.section];
    LCSubDetailModel *detailModel = topModel.detailList[indexPath.row];
    LCDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LCDetailCell" forIndexPath:indexPath];
    [cell showModel:detailModel];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 85;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label;
    if (section==0) {
        label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width, 80)];
        label.text = [NSString stringWithFormat:@"导读:%@",self.subModel.desc];
    }
    else
    {
        return nil;
    }
    label.adjustsFontSizeToFitWidth = YES;
    label.numberOfLines = 0;
    //label.font = [UIFont systemFontOfSize:14];
    
    label.backgroundColor = [UIColor whiteColor];
    return label;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LCSubTopListModel *topModel = self.subModel.subTopicList[indexPath.section];
    LCSubDetailModel *detailModel = topModel.detailList[indexPath.row];
    LCDetailViewController *detail = [[LCDetailViewController alloc]init];
    detail.guid = detailModel.memberItem.guid;
    detail.playBlock = ^
    {
        [self.player play];
    };
    [self presentViewController:detail animated:YES completion:nil];
}

- (IBAction)back:(id)sender {
    //收藏
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)collection:(id)sender {
}

- (IBAction)share:(id)sender {
}
@end
